using System;
using System.Windows.Forms;
using FireStarter.Base;
using FireStarterModeller.Logic;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using log4net;
using Microsoft.Practices.Unity;

namespace FireStarterModeller
{
    internal static class Program
    {
        private static readonly ILog _log = LogManager.GetLogger(typeof (Program));
        private static ProgramForm _programForm;
        private static IUnityContainer _container;
        public static string FireStarterVersion = "";
        public static string HabaneroVersion = "";
//        public static string FireStarterBuild = "";
        public const string RegisteredUserName = "";
        public static string[] CommandArgs;

        [STAThread]
        private static void Main(string[] args)
        {
            GlobalRegistry.ApplicationName = "FireStarter Modeller";
            GlobalRegistry.ApplicationVersion = "3.0 beta 3";
            CommandArgs = args;

            FireStarterVersion = GlobalRegistry.ApplicationVersion;
            Version version = typeof(BusinessObject).Assembly.GetName().Version;
            HabaneroVersion = version.Major + "." + version.Minor;

            HabaneroAppWin mainApp = new HabaneroAppWin(GlobalRegistry.ApplicationName, GlobalRegistry.ApplicationVersion)
                                         {ClassDefsXml = BOBroker.GetClassDefsXml()};
           
            //mainApp.SetPrivateKey(ResourceFireStarter.ResourceManager.GetString("PrivateKey"));
            try
            {
               
                _log.Debug(string.Format("-------- {0} {1} Starting  ---------", GlobalRegistry.ApplicationName, GlobalRegistry.ApplicationVersion));
                if (!mainApp.Startup()) return;
                if (mainApp.DatabaseConfig.Vendor.ToLower().Contains("memory"))
                {
                    DataStoreInMemory dataStoreInMemory = new DataStoreInMemory();
                    BORegistry.DataAccessor = new DataAccessorInMemory(dataStoreInMemory);
                    //TestPacks.CreateTestPack();
                }
                _container = new AutoResolvingUnityContainer();
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message);
            }
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Control.CheckForIllegalCrossThreadCalls = false;
                GlobalRegistry.UIExceptionNotifier = new ExceptionReporterNotifier();
              //   mainApp.ExceptionNotifier = new ExceptionReporterNotifier();
                 _programForm = new ProgramForm(new FireStarterFormsController(GlobalUIRegistry.ControlFactory))
                                   {IOCContainer = _container};

                 try
                {
                    Application.Run(_programForm);
                }
                catch (Exception ex)
                {
                    try
                    {
                        //TODO Mark 04 Sep 2009: See if anything decent can be done here
                        //SaveData();
                        //SaveSolution();
                    }
                    catch (Exception)
                    {
                        //Rethrow the origional error.
                        GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                    }
                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                }
            }
            catch (Exception ex)
            {
                GlobalRegistry.UIExceptionNotifier.Notify
                    (ex, "An error has occurred in the application.", "Application Error");
            }
        }
    }
}
